package pl.un4giv3n.mightvsmagic.action;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import pl.un4giv3n.mightvsmagic.entity.Action;
import pl.un4giv3n.mightvsmagic.entity.ActionType;
import pl.un4giv3n.mightvsmagic.entity.ActivityType;
import pl.un4giv3n.mightvsmagic.entity.User;
import pl.un4giv3n.mightvsmagic.entity.UserActivity;
import pl.un4giv3n.mightvsmagic.utils.ActionUtil;
import pl.un4giv3n.mightvsmagic.utils.ActivityLogUtil;

/**
 * @author andrzejewskia
 * go-to-train-jsp page action
 */
public class TrainPageAction extends PersistenceAction {

	private static final long serialVersionUID = 1L;
	/**
	 * session user
	 */
	private User user;
	private boolean showResult;
	private ActionType actionType;
	private Action currentAction;
	@Override
	public String execute() {
		EntityManager entityManager = getEntityManager();
		user = getSessionUser(entityManager);
		if (user == null) {
			addActionError("Zaloguj się");
			entityManager.close();
			return LOGIN;
		}
		currentAction = user.getCurrentAction();
		if (currentAction != null){
			actionType = currentAction.getActionType();
			if(actionType.equals(ActionType.FIGHT)) {
				if (ActionUtil.actionEnded(currentAction, user)) {
					EntityTransaction transaction = entityManager
							.getTransaction();
					transaction.begin();
					user.setCurrentAction(Action.NO_ACTION);
					entityManager.merge(user);
					entityManager.flush();
					transaction.commit();
					currentAction = Action.NO_ACTION;
				}
			}else if(isFightTraining() || isMagicTraining()){
				if (ActionUtil.actionEnded(currentAction, user)) {
					entityManager.getTransaction().begin();
					if (isFightTraining()) {
						user.addFightExp(currentAction.getReward()
										.getRewardedFightExp());
					} else if (isMagicTraining()) {
						user.addMagicExp(currentAction.getReward()
										.getRewardedMagicExp());
					}
					user.setCurrentAction(Action.NO_ACTION);
					StringBuilder msg = new StringBuilder("Zdobyte doświadczenie bitewne=");
					msg.append(currentAction.getReward().getRewardedFightExp());
					msg.append(", doświadczenie magiczne=").append(currentAction.getReward().getRewardedMagicExp());
					UserActivity activity = ActivityLogUtil.createActivity(ActivityType.TRAIN_END, user.getId(), msg.toString());
					entityManager.persist(activity);
					entityManager.merge(user);
					entityManager.flush();
					entityManager.getTransaction().commit();
					entityManager.close();
					showResult = true;
					return ACTION_END;
				}
			}
		}
		entityManager.close();
		if (currentAction == null || currentAction.equals(Action.NO_ACTION)) {
			return SUCCESS;
		} else {
			addActionError(ActionUtil.generateErrorMessage(currentAction, user));
			return INPUT;
		}
	}

	public User getUser() {
		return user;
	}
	public boolean isShowResult() {
		return showResult;
	}

	public void setShowResult(boolean showResult) {
		this.showResult = showResult;
	}
	
	private boolean isMagicTraining() {
		return actionType == ActionType.TRAIN_MAGIC;
	}
	
	private boolean isFightTraining() {
		return actionType == ActionType.TRAIN_FIGHT;
	}
	
	public long getExp() {
		return isFightTraining() ? currentAction.getReward()
				.getRewardedFightExp() : currentAction.getReward()
				.getRewardedMagicExp();
	}

	public String getExpType() {
		return isFightTraining() ? "bitewne" : "magiczne";
	}
	
	public String getActionEnd(){
		return ActionUtil.generateStartMessage(currentAction, user);
	}
	
	public String getActionEndString(){
		return ActionUtil.getActionEndDate(currentAction, user);
	}

}
